package com.wzh.bargain.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

import com.alibaba.fastjson.JSON;
import com.wzh.bargain.entity.GoodsEntity;
import com.wzh.bargain.feign.GoodsFeign;
import com.wzh.bargain.service.BarLogService;
import com.wzh.common.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wzh.bargain.entity.BargainActivityEntity;
import com.wzh.bargain.service.BargainActivityService;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author wzh
 * @email 1281204121@qq.com
 * @date 2022-03-08 16:12:49
 */
@RestController
@RequestMapping("bargain/bargainactivity")
public class BargainActivityController {
    @Autowired
    private BargainActivityService bargainActivityService;

    @Autowired
    private GoodsFeign goodsFeign;

    @Autowired
    private BarLogService barLogService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bargainActivityService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 生成分享链接
     */
    @RequestMapping("/getShareUrl/{bargainId}")
    public R getShareUrl(@PathVariable("bargainId") String bargainId, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token!=null){
            Claims claims = jwtUtil.parseJWT(token);
            if (claims!=null&&"admin".equals(claims.get("roles"))){
                String path = jwtUtil.createJWT(claims.getId(), bargainId, "user");
                return R.ok().put("shareUrl","http://127.0.0.1:8888/api/bargain/bargainactivity/"+path+"/bargaining");
            }
            return R.error("用户未登录");
        }
        return R.error("用户未登录");
    }
    //进行砍价
    @RequestMapping("/{path}/bargaining/{bargainId}")
    public R bargaining(@PathVariable("path") String path,HttpServletRequest request,@PathVariable("bargainId") String bargainId){
        //判断登陆者
        String token = request.getHeader("token");
        if (token!=null){
            Claims claims = jwtUtil.parseJWT(token);
            if (claims!=null&&"admin".equals(claims.get("roles"))) {
                String userId=claims.getId();
                //判断被砍价者
                Claims claims2 = jwtUtil.parseJWT(path);
                    if (claims2!=null&&"user".equals(claims2.get("roles"))) {
                        if (!claims2.getId().equals(userId)){
                            if (claims2.getSubject().equals(bargainId)) {
                                return barLogService.bargaining(userId,claims2.getId(),bargainId);
                            }else {
                                return R.error("活动不匹配");
                            }
                        }else {
                            return R.error("不能为自己砍价");
                        }
                    }
                    return R.error("砍价链接有误");
            }
            return R.error("未登录,请先登录");
        }
        return R.error("未登录,请先登录");
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{bargainId}")
    public R info(@PathVariable("bargainId") Integer bargainId){
		BargainActivityEntity bargainActivity = bargainActivityService.getById(bargainId);

        return R.ok().put("bargainActivity", bargainActivity);
    }

    /**
     * 信息
     */
    @RequestMapping("/info")
    public R info(@RequestParam("bargainId") Integer bargainId,@RequestParam("goodId") Long goodId){
        //查询商品以及活动时间
        R info = goodsFeign.info(goodId);
        if (info.getCode()==0){
            Object goods = info.get("goods");
            String jsonString = JSON.toJSONString(goods);
            GoodsEntity good = JSON.parseObject(jsonString, GoodsEntity.class);
            BargainActivityEntity bargainActivity = bargainActivityService.getById(bargainId);
            return R.ok().put("bargainActivity", bargainActivity)
                    .put("goods",good)
                    .put("endTime",bargainActivity.getEndDate().getTime())
                    .put("startTime",bargainActivity.getStartDate().getTime());
        }
        return R.error("远程调用失败");
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BargainActivityEntity bargainActivity){
		bargainActivityService.save(bargainActivity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BargainActivityEntity bargainActivity){
		bargainActivityService.updateById(bargainActivity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] bargainIds){
		bargainActivityService.removeByIds(Arrays.asList(bargainIds));

        return R.ok();
    }

}
